package com.thinkgem.jeesite.modules.book.service;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.IdGen;
import com.thinkgem.jeesite.modules.book.dao.BookMgrDao;
import com.thinkgem.jeesite.modules.book.dao.BookPurchaseDao;
import com.thinkgem.jeesite.modules.book.dao.BookStoreDao;
import com.thinkgem.jeesite.modules.book.entity.BkBookinfo;
import com.thinkgem.jeesite.modules.book.entity.SelfBookSearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 流程模型相关Controller
 * @author ThinkGem
 * @version 2013-11-03
 */
@Service
@Transactional(readOnly = true)
public class BookMgrService extends CrudService<BookMgrDao, BkBookinfo> {

    @Autowired
    private BookPurchaseDao bookPurchaseDao;

    @Autowired
    private BookStoreDao bookStoreDao;

    /**
     * 保存数据（插入或更新）
     * @param bookinfo
     */
    @Transactional()
    public void saveBookInfo(BkBookinfo bookinfo) {
        if ("0".equals(bookinfo.getId())){
            bookinfo.preInsert();
            bookinfo.setId(IdGen.getUniqueString());
            dao.insert(bookinfo);
        }else{
            bookinfo.preUpdate();
            dao.update(bookinfo);
            // 当图书信息修改时，同时
            bookPurchaseDao.updateByBkId(bookinfo);
            bookStoreDao.updateByBkId(bookinfo);
        }
    }
    /**
     * 保存数据
     *
     * @param bookinfo bookinfo
     */
    @Transactional()
    public void insert(BkBookinfo bookinfo){
        dao.insert(bookinfo);
    }

    public List<BkBookinfo> getBookInfosByIsbn(String isbn){
        return dao.getByIsbn(isbn);
    }

    /**
     * 查询分页数据
     * @param page 分页对象
     * @param entity
     * @return
     */
    public Page<BkBookinfo> findBorrowPage(Page<BkBookinfo> page, BkBookinfo entity) {
        entity.setPage(page);
        page.setList(dao.findBorrowPage(entity));
        return page;
    }

    public BkBookinfo getByIsbn(String isbn){
        List<BkBookinfo> list = dao.getByIsbn(isbn);
        if(list != null && !list.isEmpty()){
            return list.get(0);
        }
        else{
            return new BkBookinfo();
        }
    }

    public BkBookinfo getBybanid(String banid){
        return dao.getBybanid(banid);
    }

    /**
     * 查询分页数据（前台查询馆藏书籍）
     * @param page 分页对象
     * @param entity
     * @return
     */
    public Page<BkBookinfo> findFrontSearchPage(Page<BkBookinfo> page, SelfBookSearch entity) {
        entity.setPage(page);
        page.setList(dao.findListByfront(entity));
        return page;
    }

    public BkBookinfo findFrontSearchByBookId(String bookId){
        SelfBookSearch entity = new SelfBookSearch();
        entity.setBookId(bookId);
        List<BkBookinfo> listByfront = dao.findListByfront(entity);
        if(CollectionUtils.isEmpty(listByfront)){
            return null;
        }
        else{
            return listByfront.get(0);
        }
    }

    public BkBookinfo findFrontSearchByBanId(String banId){
        SelfBookSearch entity = new SelfBookSearch();
        entity.setBanId(banId);
        List<BkBookinfo> listByfront = dao.findListByfront(entity);
        if(CollectionUtils.isEmpty(listByfront)){
            return null;
        }
        else{
            return listByfront.get(0);
        }
    }
}
